<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>DegreesOfSeparation.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="DegreesOfSeparation code in Java">
<meta NAME="TITLE" CONTENT="DegreesOfSeparation code in Java">
<meta NAME="KEYWORDS" CONTENT="DegreesOfSeparation,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>DegreesOfSeparation.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "DegreesOfSeparation.java">DegreesOfSeparation.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac DegreesOfSeparation.java</span>
<span class="comment"> *  Execution:    java DegreesOfSeparation filename delimiter source</span>
<span class="comment"> *  Dependencies: SymbolGraph.java Graph.java BreadthFirstPaths.java StdOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/41graph/routes.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/41graph/movies.txt</span>
<span class="comment"> *  </span>
<span class="comment"> *  </span>
<span class="comment"> *  %  java DegreesOfSeparation routes.txt " " "JFK"</span>
<span class="comment"> *  LAS</span>
<span class="comment"> *     JFK</span>
<span class="comment"> *     ORD</span>
<span class="comment"> *     DEN</span>
<span class="comment"> *     LAS</span>
<span class="comment"> *  DFW</span>
<span class="comment"> *     JFK</span>
<span class="comment"> *     ORD</span>
<span class="comment"> *     DFW</span>
<span class="comment"> *  EWR</span>
<span class="comment"> *     Not in database.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java DegreesOfSeparation movies.txt "/" "Bacon, Kevin"</span>
<span class="comment"> *  Kidman, Nicole</span>
<span class="comment"> *     Bacon, Kevin</span>
<span class="comment"> *     Woodsman, The (2004)</span>
<span class="comment"> *     Grier, David Alan</span>
<span class="comment"> *     Bewitched (2005)</span>
<span class="comment"> *     Kidman, Nicole</span>
<span class="comment"> *  Grant, Cary</span>
<span class="comment"> *     Bacon, Kevin</span>
<span class="comment"> *     Planes, Trains &amp; Automobiles (1987)</span>
<span class="comment"> *     Martin, Steve (I)</span>
<span class="comment"> *     Dead Men Don't Wear Plaid (1982)</span>
<span class="comment"> *     Grant, Cary</span>
<span class="comment"> *</span>
<span class="comment"> *  % java DegreesOfSeparation movies.txt "/" "Animal House (1978)"</span>
<span class="comment"> *  Titanic (1997)</span>
<span class="comment"> *     Animal House (1978)</span>
<span class="comment"> *     Allen, Karen (I)</span>
<span class="comment"> *     Raiders of the Lost Ark (1981)</span>
<span class="comment"> *     Taylor, Rocky (I)</span>
<span class="comment"> *     Titanic (1997)</span>
<span class="comment"> *  To Catch a Thief (1955)</span>
<span class="comment"> *     Animal House (1978)</span>
<span class="comment"> *     Vernon, John (I)</span>
<span class="comment"> *     Topaz (1969)</span>
<span class="comment"> *     Hitchcock, Alfred (I)</span>
<span class="comment"> *     To Catch a Thief (1955)</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">DegreesOfSeparation</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides a client for finding</span>
<span class="comment"> *  the degree of separation between one distinguished individual and</span>
<span class="comment"> *  every other individual in a social network.</span>
<span class="comment"> *  As an example, if the social network consists of actors in which</span>
<span class="comment"> *  two actors are connected by a link if they appeared in the same movie,</span>
<span class="comment"> *  and Kevin Bacon is the distinguished individual, then the client</span>
<span class="comment"> *  computes the Kevin Bacon number of every actor in the network.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The running time is proportional to the number of individuals and</span>
<span class="comment"> *  connections in the network. If the connections are given implicitly,</span>
<span class="comment"> *  as in the movie network example (where every two actors are connected</span>
<span class="comment"> *  if they appear in the same movie), the efficiency of the algorithm</span>
<span class="comment"> *  is improved by allowing both movie and actor vertices and connecting</span>
<span class="comment"> *  each movie to all of the actors that appear in that movie.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/41graph"</span><span class="keyword">&gt;</span><span class="comment">Section 4.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">DegreesOfSeparation</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">// this class cannot be instantiated</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">DegreesOfSeparation</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  Reads in a social network from a file, and then repeatedly reads in</span>
<span class="comment">     *  individuals from standard input and prints out their degrees of</span>
<span class="comment">     *  separation.</span>
<span class="comment">     *  Takes three command-line arguments: the name of a file,</span>
<span class="comment">     *  a delimiter, and the name of the distinguished individual.</span>
<span class="comment">     *  Each line in the file contains the name of a vertex, followed by a</span>
<span class="comment">     *  list of the names of the vertices adjacent to that vertex,</span>
<span class="comment">     *  separated by the delimiter.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> filename  </span><span class="symbol">=</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">];</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> delimiter </span><span class="symbol">=</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> source    </span><span class="symbol">=</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">// StdOut.println("Source: " + source);</span>

<span class="normal">        </span><span class="usertype">SymbolGraph</span><span class="normal"> sg </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SymbolGraph</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">,</span><span class="normal"> delimiter</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> sg</span><span class="symbol">.</span><span class="function">G</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">sg</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">source</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">source </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" not in database."</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="type">int</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> sg</span><span class="symbol">.</span><span class="function">index</span><span class="symbol">(</span><span class="normal">source</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">BreadthFirstPaths</span><span class="normal"> bfs </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BreadthFirstPaths</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">StdIn</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">String</span><span class="normal"> sink </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">sg</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">sink</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> sg</span><span class="symbol">.</span><span class="function">index</span><span class="symbol">(</span><span class="normal">sink</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">bfs</span><span class="symbol">.</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">:</span><span class="normal"> bfs</span><span class="symbol">.</span><span class="function">pathTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"   "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> sg</span><span class="symbol">.</span><span class="function">name</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">));</span>
<span class="normal">                    </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Not connected"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"   Not in database."</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
